package com.libweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libweb.model.dao.BookDao;
import com.libweb.model.dto.Book;

@WebServlet("/book/list.action")
public class BookListServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		/*
		//0. 로그인 되지 않은 사용자는 로그인 화면으로 이동
		if(req.getSession().getAttribute("loginuser") == null) {
			resp.sendRedirect(
					"/libweb/account/loginform.action?returnurl=" + req.getRequestURL());//현재요청에 대한 경로 문자열로 반환
			return;
		}
		*/
		//1. 요청 분석 및 수신 데이터 읽기
		
		//2. 요청 처리
		BookDao dao = new BookDao();
		List<Book> books = dao.getBooks();
		
		//Servlet에서 처리된 데이터를 JSP에 전달 (request 객체 이용)
		req.setAttribute("books", books);
		
		//3-2. 응답 컨텐츠 생성 (JSP 이용해서 처리 - jsp page로 forward)
		RequestDispatcher dispatcher = 
			req.getRequestDispatcher("/WEB-INF/views/book/list.jsp");

		dispatcher.forward(req, resp);
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		doGet(req, resp);
	}
	
}






